package com.juzhi.dvrapp.activity;

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.juzhi.dvrapp.R;
import com.juzhi.dvrapp.utils.Config;
import com.juzhi.dvrapp.utils.MLog;
import com.juzhi.dvrapp.utils.Urls;
import com.juzhi.dvrapp.videoplay.audio.AudioRecorder;
import com.juzhi.dvrapp.videoplay.audio.RecordButton;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * 语音对讲   语音对讲只能在流启动之后 设备端才能播放语音
 */

public class DeviceTalkActivity extends BaseAcitivity implements AudioRecorder.RecordListener {


    private RecordButton mRecordBtn;
    private LinearLayout mRecordDlg;
    private ImageView mRecordImg;
    private TextView mRecordText;
    private long mSeq = 1;
    private List<String> mTalkCmdDataList = new ArrayList<String>();
    private boolean mIsTalking = false;
    private String ssid;
    private static boolean mSendingTalkData;

    @Override
    public int getLayoutId() {
        return R.layout.activity_device_talk;
    }

    @Override
    protected void initView() {
        ssid = getIntent().getStringExtra(Config.SSID);

        mRecordImg = (ImageView) this.findViewById(R.id.record_dialog_img);
        mRecordText = (TextView) this.findViewById(R.id.record_dialog_txt);
        mRecordDlg = (LinearLayout) this.findViewById(R.id.dlgRecord);
        mRecordBtn = (RecordButton) this.findViewById(R.id.btRecordBtn);
        mRecordBtn.init(this, mRecordDlg, mRecordImg, mRecordText);
        AudioRecorder audioRecord = new AudioRecorder();
        audioRecord.setRecordListener(this);
        mRecordBtn.setAudioRecord(audioRecord);
    }

    @Override
    public void recordStart(long presentationTimeStamp) {
        mSeq = 1;
        mTalkCmdDataList.clear();
        mIsTalking = true;
        buildTalkBackCommand("START", presentationTimeStamp, "");
        startSendTalkCmdData();
    }

    @Override
    public void recordSendData(String data, long presentationTimeStamp) {
        buildTalkBackCommand("SENDDATA", presentationTimeStamp, data);
    }

    @Override
    public void recordEnd(long presentationTimeStamp) {
        buildTalkBackCommand("STOP", presentationTimeStamp, "");
        mIsTalking = false;
    }


    private void buildTalkBackCommand(String cmd, long pts, String data) {
        String json = String.format("{\n" +
                "  \"EasyDarwin\": {\n" +
                "    \"Body\": {\n" +
                "      \"Channel\": \"%d\",\n" +
                "      \"Protocol\": \"ONVIF\",\n" +
                "      \"Reserve\": \"1\",\n" +
                "      \"Command\": \"%s\",\n" +
                "      \"AudioType\": \"G711A\",\n" +
                "      \"Pts\": \"%d\",\n" +
                "      \"AudioData\": \"%s\",\n" +
                "      \"Serial\": \"%s\"\n" +
                "    },\n" +
                "    \"Header\": {\n" +
                "      \"CSeq\": \"%d\",\n" +
                "      \"MessageType\": \"MSG_CS_TALKBACK_CONTROL_REQ\",\n" +
                "      \"Version\": \"1.0\"\n" +
                "    }\n" +
                "  }\n" +
                "}", 1, cmd, pts, data, ssid, mSeq++);

        synchronized (mTalkCmdDataList) {
            mTalkCmdDataList.add(json);
        }

        return;
    }

    private void startSendTalkCmdData() {
        Thread th = new Thread(new Runnable() {
            @Override
            public void run() {
                mSendingTalkData = false;

                int i = 0;
                while (mIsTalking || i < mTalkCmdDataList.size()) {
                    if (false == mSendingTalkData && i < mTalkCmdDataList.size()) {
                        String data;
                        synchronized (mTalkCmdDataList) {
                            data = mTalkCmdDataList.get(i++);
                        }
                        MLog.d("TAG", "kim send data : " + data);
                        mSendingTalkData = true;
                        OkGo.<String>post(Urls.CMS_SERVER)
                                .upString(data)
                                .execute(new StringCallback() {
                                    @Override
                                    public void onSuccess(Response<String> response) {
                                        MLog.d("Start onSuccess");
                                        mSendingTalkData = false;
                                    }

                                    @Override
                                    public void onError(Response<String> response) {
                                        super.onError(response);
                                        MLog.d("Start onError");
                                        mSendingTalkData = false;
                                    }
                                });
                    }
                }

                mTalkCmdDataList.clear();
            }
        });
        th.start();
    }
}
